// PixelationPostProcessing.shader
Shader "Custom/PixelationPostProcessing"
{
    Properties
    {
        _MainTex ("Screen Texture", 2D) = "white" {}
        _PixelSize ("Pixel Size", Range(1, 100)) = 10
    }
    
    SubShader
    {
        Tags { "RenderType"="Opaque" "Queue"="Overlay" }
        LOD 100
        
        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            #include "UnityCG.cginc"
            
            sampler2D _MainTex;
            float4 _MainTex_TexelSize;
            float _PixelSize;
            
            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };
            
            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
            };
            
            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                return o;
            }
            
            fixed4 frag (v2f i) : SV_Target
            {
                // 计算像素化后的UV坐标
                float2 pixelatedUV = floor(i.uv * _PixelSize) / _PixelSize;
                
                // 采样像素化后的颜色
                fixed4 color = tex2D(_MainTex, pixelatedUV);
                
                return color;
            }
            ENDCG
        }
    }
}
